package com.hmall.item.web;

import com.aliyuncs.exceptions.ClientException;
import com.hmall.common.PageBean;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;
import com.hmall.item.service.SearchService;
import com.hmall.parmas.Parmas;
import com.hmall.search.dto.SearchDto;
import com.hmall.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {
    @Autowired
    ItemService itemService;

    @GetMapping("list")
    public PageBean<Item> pageQuery(Integer page, Integer size) {
        PageBean<Item> pageBean = itemService.pageQuery(page, size);
        return pageBean;

    }

    @PutMapping
    public void updateItem(@RequestBody Item item) {
        itemService.updateItem(item);
    }

    @PostMapping
    public void addItem(@RequestBody Item item) {
        itemService.addItem(item);
    }

    @PutMapping("status/{id}/{status}")
    public void Byid(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.updateStatus(item);
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable("id") Long id) {

        itemService.deleteById(id);

    }
    @GetMapping("{id}")
    public Item getById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }


    @PostMapping("upload")
    public String upload(MultipartFile file) throws ClientException, IOException {
        System.out.println(file);
      String url=  AliOssUtil.upload(file.getInputStream());
        System.out.println("=========================================" + url);
        return url;
    }
    @GetMapping("filterList")
    public List<String[]>filterList(){
        return itemService.filterList();
    }
    @GetMapping("itemboxlist")
    public PageBean<Item>itemboxlist(Integer page, Integer size,String sortBy){
        PageBean<Item> pageBean = itemService.pageQuery(page, size, sortBy);
        return pageBean;
    }
    @Autowired
    SearchService searchService;
    @GetMapping("filterSearch")
    public PageBean<Item>filterSearch(@PathVariable Integer page, @PathVariable Integer size,@PathVariable String sortBy,@PathVariable String category,@PathVariable String brand){
        PageBean<Item> pageBean=searchService.filterSearch(page,size,sortBy,category,brand);
        System.out.println(pageBean);
        return pageBean;
    }
    @PostMapping("/stock/{itemId}/{num}")
    public  void updatestock(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.updatestock(itemId,num);
    }

}





